package com.youboy.wechatapi.web.Interceptors;

import java.util.concurrent.LinkedBlockingQueue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * @ProjectName：wechatapi   
 * @author：冉尧（Yvan）
 * @CreateTime：2014年10月11日 上午9:42:34  
 * @Remarks：   
 */
public abstract class AbstractLogInterceptor<T> extends HandlerInterceptorAdapter implements InitializingBean{
	LinkedBlockingQueue<T> queue = null;
	

	public abstract boolean accept(HttpServletRequest request,
			HttpServletResponse response);
	public abstract void doWith(T t);
	public void addLog(T t){
		queue.add(t);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		queue = new LinkedBlockingQueue<T>();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true){
					try {
						T t = queue.take();
						doWith(t);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		}).start();
	}
}


